package org.edu.modules.classChoise.controller;

import org.edu.modules.classChoise.vo.VeJwSxjxVO;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.classChoise.entity.VeJwSxjx;
import org.edu.modules.classChoise.entity.VeJwXkdj;
import org.edu.modules.classChoise.service.IVeJwSxjxService;
import org.edu.modules.classChoise.service.IVeJwSxjxsecService;
import org.edu.modules.classChoise.service.IVeJwSxjxstuService;
import org.edu.modules.classChoise.entity.VeJwSxjxsec;
import org.edu.modules.classChoise.entity.VeJwSxjxstu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @Description: 选修教学
* @Author:
* @Date:   2021-05-14
* @Version: V1.0
*/
@Api(tags="选修教学")
@RestController
@RequestMapping("/classChoise/veJwSxjx")
@Slf4j
public class VeJwSxjxController extends BaseController<VeJwSxjx, IVeJwSxjxService> {
   @Autowired
   private IVeJwSxjxService veJwSxjxService;
    @Autowired
    private IVeJwSxjxsecService veJwSxjxsecService;
    @Autowired
    private IVeJwSxjxstuService veJwSxjxstuService;


   /**
    * 分页查询
    * @param veJwSxjx
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "选修教学-分页查询")
   @ApiOperation(value="选修教学分页查询", notes="选修教学-分页查询")
   @PostMapping(value = "/list")
   public Result<?> queryPageList( VeJwSxjx veJwSxjx,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {

       QueryWrapper<VeJwSxjx> queryWrapper = QueryGenerator.initQueryWrapper(veJwSxjx, req.getParameterMap());

    /*   String name= veJwSxjx.getName();//名称
       if(name!=null&& !name.equals("")) {
           queryWrapper.like("name", name);
       }



       Integer gradeid= veJwSxjx.getGradeid();//年级id
       if(gradeid!=null&& !gradeid.equals("")) {
           queryWrapper.eq("gradeid", gradeid);
       }
       Integer semid= veJwSxjx.getSemid();//学期ID
       if(semid!=null&& !semid.equals("")) {
           queryWrapper.eq("semid", semid);
       }
       Integer specid= veJwSxjx.getSpecid();//名称
       if(specid!=null&& !specid.equals("")) {
           queryWrapper.eq("specid", specid);
       }
       Integer falid= veJwSxjx.getFalid();//名称
      if(falid!=null&& !falid.equals("")) {
           queryWrapper.eq("falid", falid);
       }*/
       Page<VeJwSxjx> page = new Page<VeJwSxjx>(pageNo, pageSize);
       IPage<VeJwSxjx> pageList = veJwSxjxService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

    /**
     * 分页查询
     * @param sxjxid
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "教学班学员-分页查询")
    @ApiOperation(value="教学班学员-分页查询", notes="教学班学员-分页查询")
    @PostMapping(value = "/stulist")
    public Result<?> queryPageStuList(@RequestParam(name="sxjxid",required=true) String sxjxid,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {

        QueryWrapper<VeJwSxjx> queryWrapper = new  QueryWrapper<VeJwSxjx>();


        if(sxjxid!=null&& !sxjxid.equals("")) {
            queryWrapper.eq("b.sxjxid", sxjxid);
        }

        Page<Map<String,Object>> page = new Page<Map<String,Object>>(pageNo, pageSize);
        IPage<Map<String,Object>> pageList = veJwSxjxService.pageMaps(page, queryWrapper);


        //  Page<VeJwSxjxstu> page = new Page<VeJwSxjxstu>(pageNo, pageSize);
        //  IPage<VeJwSxjxstu> pageList = veJwSxjxstuService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 所有教学班信息
     * @return
     */
    @AutoLog(value = "所有教学班")
    @ApiOperation(value="所有教学班", notes="所有教学班-显示")
    @PostMapping(value = "/sxjxlist")

    public Result<?> sxjxlist(
                                HttpServletRequest req ) {
        List<Map<String, Object>> list = veJwSxjxService.selectMapsPage1();
        return Result.OK(list);
    }
   /**
   * 通过id查询
   * @param id
   * @return
   */
   @AutoLog(value = "选课时间-通过id查询")
   @ApiOperation(value="通过id查询", notes="选课时间-通过id查询")
   @PostMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {

           QueryWrapper<VeJwSxjx> queryWrapper = new  QueryWrapper<VeJwSxjx>();


           if(id!=null&& !id.equals("")) {
               queryWrapper.eq("id", id);
           }
           VeJwSxjx veJwSxjx = veJwSxjxService.getOne(queryWrapper);



       if(veJwSxjx==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(veJwSxjx);
   }

    /**
     * 修改信息
     * @param veJwSxjxsec
     * @return
     * 没用了
     */
    @AutoLog(value = "选修教学设置节")
    @ApiOperation(value="选修教学设置节", notes="选修教学设置节")
    @PostMapping(value = "/setNoScheduleForClass")
    public Result<?> setNoScheduleForClass(@RequestBody List<VeJwSxjxsec> veJwSxjxsec,
                                             @RequestParam(name="sxjsId",required=true) String sxjsId)
    {
        if(veJwSxjxService.setScheduleForClass(veJwSxjxsec,sxjsId)){
            return Result.OK("设置成功!");
        }
        return Result.error("设置失败!");
    }


   /**
    * 新增信息
    * @param veJwSxjxVO
    * @return
    */
   @AutoLog(value = "选修教学-新增信息")
   @ApiOperation(value="选修教学新增信息", notes="选修教学-新增信息")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody VeJwSxjxVO veJwSxjxVO) {
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

       //if(sysUser!=null){
         //  veJwSxjxVO.setCreateuserid(sysUser.getId());
       //}
       if (veJwSxjxVO.getClassroomid()!=null){
           String  classname=veJwSxjxService.selectjiaoshiName(veJwSxjxVO.getClassroomid());
           if (classname!=null)
               veJwSxjxVO.setJsmc(classname);
           else
               veJwSxjxVO.setJsmc("");}
       else
           veJwSxjxVO.setJsmc("");


       veJwSxjxVO.setCreatetime(new Date());

       if (veJwSxjxVO.getSpecid()!=null){
           String  specname=veJwSxjxService.selectspecName(veJwSxjxVO.getSpecid());
           if (specname!=null)
               veJwSxjxVO.setSpecname(specname);
           else
               veJwSxjxVO.setSpecname("");}
       else
           veJwSxjxVO.setSpecname("");


       if (veJwSxjxVO.getGradeid()!=null){
       String  gradename=veJwSxjxService.selectgradeName(veJwSxjxVO.getGradeid());
if (gradename!=null)
       veJwSxjxVO.setGradename(gradename);
       else
    veJwSxjxVO.setGradename("");}
       else
           veJwSxjxVO.setGradename("");


       if (veJwSxjxVO.getTeacherid()!=null){
           String  teachername=veJwSxjxService.selectteacherName(veJwSxjxVO.getGradeid());
           if (teachername!=null)
               veJwSxjxVO.setTeachername(teachername);
           else
               veJwSxjxVO.setTeachername("");}
       else
           veJwSxjxVO.setTeachername("");


       if(veJwSxjxService.save(veJwSxjxVO)){
           return Result.OK("添加成功！",veJwSxjxVO.getId());
       }

       return Result.error("添加失败!");
   }
   /*
    @AutoLog(value = "选修教学设置学生")
    @ApiOperation(value="选修教学设置学生", notes="选修教学设置学生")
    @PostMapping(value = "/setNoSxjxForStu")
    public Result<?> setNoScheduleForStu(@RequestParam(name="sxjxId",required=true) int sxjxId,
                                         @RequestParam(name="strs",required=true) String strs)
    {
        if(veJwSxjxstuService.delSxjxForStu(strs.split(","),sxjxId)){
            return Result.OK("设置成功!");
        }
        return Result.error("设置失败!");
    }
*/
   /**
    * 修改信息
    * @param veJwSxjxVO
    * @return
    */
   @AutoLog(value = "选课时间-修改信息")
   @ApiOperation(value="修改信息", notes="选课时间-修改信息")
   @PostMapping(value = "/edit")
   public Result<?> edit(@RequestBody VeJwSxjxVO veJwSxjxVO) {
       veJwSxjxVO.setUpdatetime(new Date());


       if (veJwSxjxVO.getClassroomid()!=null){
           String  classname=veJwSxjxService.selectjiaoshiName(veJwSxjxVO.getClassroomid());
           if (classname!=null)
               veJwSxjxVO.setJsmc(classname);
           else
               veJwSxjxVO.setJsmc("");}
       else
           veJwSxjxVO.setJsmc("");



       if (veJwSxjxVO.getSpecid()!=null){
           String  specname=veJwSxjxService.selectspecName(veJwSxjxVO.getSpecid());
           if (specname!=null)
               veJwSxjxVO.setSpecname(specname);
           else
               veJwSxjxVO.setSpecname("");}
       else
           veJwSxjxVO.setSpecname("");


       if (veJwSxjxVO.getGradeid()!=null){
           String  gradename=veJwSxjxService.selectgradeName(veJwSxjxVO.getGradeid());
           if (gradename!=null)
               veJwSxjxVO.setGradename(gradename);
           else
               veJwSxjxVO.setGradename("");}
       else
           veJwSxjxVO.setGradename("");
       if(veJwSxjxService.updateById(veJwSxjxVO)){
           return Result.OK("编辑成功！",veJwSxjxVO.getId());

       }
       return Result.error("编辑失败!");
   }


    /**
     * 所有教材信息
     * @return
     */
    @AutoLog(value = "所有教材")
    @ApiOperation(value="所有教材", notes="所有教材-显示")
    @PostMapping(value = "/jiaocailist")

    public Result<?> jiaocailist(
            HttpServletRequest req ) {
        List<Map<String, Object>> list = veJwSxjxService.selectJiaocaiPage();
        return Result.OK(list);
    }

    /**
     * 某一选修教学班的课
     * @return
     */
    @AutoLog(value = "某一选修教学班的课")
    @ApiOperation(value="某一选修教学班的课", notes="选修教学班的课-显示")
    @PostMapping(value = "/sxjxseclist")

    public Result<?> sxjxseclist(@RequestParam(name="sxjxId",required=true) String sxjxId
            ) {
        List<Map<String, Object>> list = veJwSxjxService.selectSxjxsecPage(sxjxId);
        return Result.OK(list);
    }
    /**
     * 所有教材信息
     * @return
     */
    @AutoLog(value = "所有课程")
    @ApiOperation(value="所有课程", notes="所有课程-显示")
    @PostMapping(value = "/courselist")

    public Result<?> courselist(
            HttpServletRequest req ) {
        List<Map<String, Object>> list = veJwSxjxService.selectCoursePage();
        return Result.OK(list);
    }
//	/**
//	 * 通过id删除
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "选课时间-通过id删除")
//	@ApiOperation(value="通过id删除", notes="选课时间-通过id删除")
//	@PostMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//        if(veJwSxjxService.removeById(id)){
//		    return Result.OK("删除成功!");
//        }
//        return Result.error("删除失败!");
//	}

   /**
    * 批量删除
    * @param ids
    * @return
    */
   @AutoLog(value = "选课时间-批量销毁")
   @ApiOperation(value="批量销毁", notes="选课时间-批量销毁")
   @PostMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       if(this.veJwSxjxService.removeByIds(Arrays.asList(ids.split(",")))){
                return Result.OK("批量销毁成功!");
       }
       return Result.error("批量销毁失败!");
   }



    /**
     * 导出excel
     *
     * @param request
     * @param veJwSxjx
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request,   VeJwSxjx veJwSxjx) {
        return super.exportXls(request, veJwSxjx, VeJwSxjx.class, "选课时间信息");
    }

//     /**
//      * 导出excel
//      *
//      * @param request
//      * @param veJwSxjx
//      */
//     @AutoLog(value = "选课时间-导出Excel")
//     @ApiOperation(value = "导出Excel", notes = "选课时间-导出Excel")
//     @GetMapping(value = "/exportXls")
//     //@RequestMapping(value = "/exportXls")
//     public ModelAndView exportXls(HttpServletRequest request, VeJwSxjx veJwSxjx) {
//         // Step.1 组装查询条件
//         QueryWrapper<VeJwSxjx> queryWrapper = QueryGenerator.initQueryWrapper(veJwSxjx, request.getParameterMap());
//         //Step.2 AutoPoi 导出Excel
//         ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//         List<VeJwSxjx> pageList = veJwSxjxService.list(queryWrapper);
//         //导出文件名称
//         mv.addObject(NormalExcelConstants.FILE_NAME,"选课时间");
//         mv.addObject(NormalExcelConstants.CLASS,VeJwSxjx.class);
//         //LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//         //mv.addObject(NormalExcelConstants.PARAMS,new ExportParams("角色列表数据","导出人:"+user.getRealname(),"导出信息"));
//         mv.addObject(NormalExcelConstants.DATA_LIST,pageList);
//         return mv;
//         //return super.exportXls(request, veJwXkxz, VeJwXkxz.class, "教务课程信息");
//     }

}
